#include <iostream>
#include <vector>
#include <set>

int thirdMax(std::vector<int>& nums) {
    std::set<int> top3;

    for (const int num : nums) {
        top3.insert(num);
        if (top3.size() > 3) {
            top3.erase(top3.begin());
        }
    }

    if (top3.size() < 3) {
        return *top3.rbegin(); 
    } else {
        return *top3.begin(); 
    }
}

int main() {
    std::vector<int> nums = {1, 2, 2, 3, 4, 5, 5};
    int result = thirdMax(nums);

    std::cout << "The third maximum number in the array is: " << result << std::endl;

    return 0;
}